<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加用附件</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
			  <el-input v-model="searchParam.access_key" placeholder="请输入access_key" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>
      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="access_key" prop="access_key"></el-table-column>
        <el-table-column label="项目编号" prop="item_id"></el-table-column>
        <el-table-column label="项目类型" prop="item_type"></el-table-column>
        <el-table-column label="项目类型" prop="item_type"></el-table-column>
        <el-table-column label="文件类型" prop="file_type"></el-table-column>
        <el-table-column label="文件名称" prop="file_name"></el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="是否有效" prop="is_valid"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加用附件" :visible.sync="showAddModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>编号：</label>
        <div class="input-group-box"><el-input v-model="addParam.id" placeholder="请输入编号"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>access_key：</label>
        <div class="input-group-box"><el-input v-model="addParam.access_key" placeholder="请输入access_key"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>项目编号：</label>
        <div class="input-group-box"><el-input v-model="addParam.item_id" placeholder="请输入项目编号"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>项目类型：</label>
        <div class="input-group-box"><el-input v-model="addParam.item_type" placeholder="请输入项目类型"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>项目类型：</label>
        <div class="input-group-box"><el-input v-model="addParam.item_type" placeholder="请输入项目类型"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>文件类型：</label>
        <div class="input-group-box"><el-input v-model="addParam.file_type" placeholder="请输入文件类型"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>文件名称：</label>
        <div class="input-group-box"><el-input v-model="addParam.file_name" placeholder="请输入文件名称"></el-input></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改附件" :visible.sync="showUpdateModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>编号：</label>
        <div class="input-group-box"><el-input v-model="editParam.id" placeholder="请输入编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>access_key：</label>
        <div class="input-group-box"><el-input v-model="editParam.access_key" placeholder="请输入access_key"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>项目编号：</label>
        <div class="input-group-box"><el-input v-model="editParam.item_id" placeholder="请输入项目编号"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>项目类型：</label>
        <div class="input-group-box"><el-input v-model="editParam.item_type" placeholder="请输入项目类型"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>项目类型：</label>
        <div class="input-group-box"><el-input v-model="editParam.item_type" placeholder="请输入项目类型"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>文件类型：</label>
        <div class="input-group-box"><el-input v-model="editParam.file_type" placeholder="请输入文件类型"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>文件名称：</label>
        <div class="input-group-box"><el-input v-model="editParam.file_name" placeholder="请输入文件名称"></el-input></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      searchParam: {
        id: '',
        access_key: ''
      },
      addParam: {
        access_key: '',
        item_id: '',
        item_type: '',
        item_type: '',
        file_type: '',
        file_name: ''
      },
      editParam: {
        id: '',
        access_key: '',
        item_id: '',
        item_type: '',
        item_type: '',
        file_type: '',
        file_name: ''
      }
    }
  },
  created () {
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        access_key: this.searchParam.access_key
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.attachList), this.params, res => {
        this.listData = res
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.id = ''
      this.searchParam.access_key = ''
      this.searchParam.item_id = ''
      this.searchParam.item_type = ''
      this.searchParam.item_type = ''
      this.searchParam.file_type = ''
      this.searchParam.file_name = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      var param = {
        id: this.addParam.id,
        access_key: this.addParam.access_key,
        item_id: this.addParam.item_id,
        item_type: this.addParam.item_type,
        item_type: this.addParam.item_type,
        file_type: this.addParam.file_type,
        file_name: this.addParam.file_name
      }
      api.GET(helper.getManageApiUrl(apiConfig.addAttach), param, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.access_key = ''
        this.addParam.item_id = ''
        this.addParam.item_type = ''
        this.addParam.item_type = ''
        this.addParam.file_type = ''
        this.addParam.file_name = ''
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteAttach), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.access_key = item.access_key
      this.editParam.item_id = item.item_id
      this.editParam.item_type = item.item_type
      this.editParam.item_type = item.item_type
      this.editParam.file_type = item.file_type
      this.editParam.file_name = item.file_name
      this.showUpdateModelFlag = true
    },
    save () {
      api.GET(helper.getManageApiUrl(apiConfig.updateAttach), this.editParam, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: '修改失败',
          type: 'error',
          duration: 1500
        })
      })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
</style>

